package com.leyou.test.controller;

import com.leyou.test.enums.ExceptionEnum;
import com.leyou.test.exceptions.LyException;
import com.leyou.test.pojo.Item;
import com.leyou.test.service.ItemService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class ItemController {
    @Autowired
    private ItemService itemService;

    /**
     * 模拟增加item
     * @param item
     * @return
     */
    @PostMapping("item")
    public ResponseEntity<Item> saveItem(Item item) {
        //判断price是否为空
        if (null == item.getPrice()) {
            //price为空
            //throw new RuntimeException("价格不能为空");
            throw new LyException(ExceptionEnum.PRICE_CONNOT_BE_NULL);
        }
        //price不为空
        Item result = itemService.saveItem(item);
        return ResponseEntity.status(HttpStatus.OK).body(result);
    }
    /*@PostMapping("item")
    public ResponseEntity<Item> saveItem(Item item) {
        //判断price是否为空
        if (null == item.getPrice()) {
            //price为空
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        //price不为空
        Item result = itemService.saveItem(item);
        return ResponseEntity.status(HttpStatus.OK).body(result);
    }*/
}
